package com.wzdigit.wms.raw.service.report;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.util.DateUtils;
import com.wzdigit.wms.common.utils.EasyExcelUtil;
import com.wzdigit.wms.raw.client.request.report.SearchPoHeaderReportRequest;
import com.wzdigit.wms.raw.client.response.report.PoDtlReportResponse;
import com.wzdigit.wms.raw.client.response.report.PoHeaderReportResponse;
import com.wzdigit.wms.raw.mapper.mid.PoDtlMapper;
import com.wzdigit.wms.raw.mapper.mid.PoHeaderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 采购订单报表-服务类
 * 
 * @author yangdongjie
 * @GdnService.java
 * @2021年2月24日下午3:25:03
 */
@Service
public class PoReportService {

	@Autowired
	private PoHeaderMapper poHeaderMapper;

	@Autowired
	private PoDtlMapper poDtlMapper;
	
	/**
	 * 采购订单报表-主档查询
	 * 
	 * @param dto
	 * @param pager
	 * @return
	 */
	public PagerInfo<List<PoHeaderReportResponse>> selectPoReportList(SearchPoHeaderReportRequest dto, Pager<?> pager) {
		Page<?> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
		Date beginApproveDate = null, endApproveDate = null, beginExpectArriveDate1 = null, endExpectArriveDate1 = null;
		if (dto.getBeginApproveTime() != null) {
			beginApproveDate = DateUtils.getStartTime(dto.getBeginApproveTime());
		}
		if (dto.getEndApproveTime() != null) {
			endApproveDate = DateUtils.getEndTime(dto.getEndApproveTime());
		}
		if (dto.getBeginExpectArriveTime1() != null) {
			beginExpectArriveDate1 = DateUtils.getStartTime(dto.getBeginExpectArriveTime1());
		}
		if (dto.getEndExpectArriveTime1() != null) {
			endExpectArriveDate1 = DateUtils.getEndTime(dto.getEndExpectArriveTime1());
		}
		PagerInfo<List<PoHeaderReportResponse>> pagerInfo = new PagerInfo<>();
		List<PoHeaderReportResponse> list = poHeaderMapper.selectAll(dto, 
				beginApproveDate, endApproveDate, beginExpectArriveDate1, endExpectArriveDate1);
		pagerInfo.setItems(list);
		pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
	}

	
	/**
	 * 采购订单报表-明细查询
	 * 
	 * @param poHeaderId
	 * @return
	 */
	public List<PoDtlReportResponse> selectPoDtlReportList(Integer poHeaderId) {
		List<PoDtlReportResponse> list = poDtlMapper.selectAllByPoHeaderId(poHeaderId);
		return list;
	}
	
	
	/**
	 * 导出主档
	 * 
	 * @param dto
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void exportPoReportList(SearchPoHeaderReportRequest dto, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Date beginApproveDate = null, endApproveDate = null, beginExpectArriveDate1 = null, endExpectArriveDate1 = null;
		if (dto.getBeginApproveTime() != null) {
			beginApproveDate = DateUtils.getStartTime(dto.getBeginApproveTime());
		}
		if (dto.getEndApproveTime() != null) {
			endApproveDate = DateUtils.getEndTime(dto.getEndApproveTime());
		}
		if (dto.getBeginExpectArriveTime1() != null) {
			beginExpectArriveDate1 = DateUtils.getStartTime(dto.getBeginExpectArriveTime1());
		}
		if (dto.getEndExpectArriveTime1() != null) {
			endExpectArriveDate1 = DateUtils.getEndTime(dto.getEndExpectArriveTime1());
		}
		List<PoHeaderReportResponse> dataSet = poHeaderMapper.selectAll(dto, 
				beginApproveDate, endApproveDate, beginExpectArriveDate1, endExpectArriveDate1);
		
		EasyExcelUtil.setResponse(request, response, "采购订单报表");
		EasyExcelUtil.export(response, PoHeaderReportResponse.class, dataSet, "1");
	}
}
